package com.taiji.dianthus.dao.sys;

import com.taiji.dianthus.domain.sys.SysDictionary;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @InterfaceName DictionaryRepository
 * @Description
 * @Author H.M
 * @Date 2020/6/4
 */
@Repository
public interface DictionaryRepository extends JpaRepository<SysDictionary, String>,
        JpaSpecificationExecutor<SysDictionary> {

    /**
     * 逻辑删除
     * @param id
     */
    @Modifying
    @Query("update SysDictionary p set p.enableFlag = 0  where p.id=:id")
    void updateFlag(@Param("id") String id);

    /**
     * 根据类型查询列表
     * @param dictType
     * @return
     */
    @Query("select  t from SysDictionary t where t.dictType=:dictType and  t.enableFlag = 1 " )
    List<SysDictionary> listByType(@Param("dictType")String dictType);

    /**
     * 根据条件获取字典属性名
     * @param value
     * @param type
     * @return
     */
    @Query("select  t.dictName from SysDictionary t where t.dictType=:dictType and t.dictValue =:dictValue and  t.enableFlag = 1 " )
    String getDictName(@Param("dictValue")String value, @Param("dictType") String type);

}
